package com.cardbaobao.cardbabyclient.ui.activity.web;

import android.content.Intent;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.buried.Google;
import com.cardbaobao.cardbabyclient.buried.Self;
import com.cardbaobao.cardbabyclient.ui.activity.base.BaseActivity;
import com.cardbaobao.cardbabyclient.buried.GoogleAnalyticsUtil;

import butterknife.BindView;

/**
 * 显示xxx协议用的webViewActivity
 * 只加载单个html页面，无js交互
 * Created by zad
 * 2017/1/14 16:12
 */
public class AgreementWebViewActivity extends BaseActivity {
    public static final String WEB_URL_S = "webUrl";
    public static final String WEB_TITLE_S = "title";
    public static final String WEB_TYPE_I = "type";
    @BindView(R.id.ll_common_container)
    LinearLayout llCommonContainer;

    @BindView(R.id.ibt_title_left)
    ImageButton ibtTitleLeft;

    @BindView(R.id.tv_title_title)
    TextView tvTitle;

    @BindView(R.id.id_web)
    WebView mWebView;

    private int mType = -1;
    //0-信用卡产品卡片介绍 1-借款介绍 2-服务协议 3-社保查询协议 4-公积金查询
    // 5-学历查询 6-消息详情 7-关于我们
    // 8-卡宝宝注册条款 9-办卡进度查询详情 10-收款协议 11-常见问题 12-信用卡还款服务协议
    // 13-使用帮助 14学信网授权协议 15公积金授权协议 16社保授权协议 17运营商授权协议

    @Override
    protected void onResume() {
        super.onResume();
        switch (mType) {
            case 0:
                GoogleAnalyticsUtil.setScreenName(Google.CREDIT_PRODUCT_INTRODUCE);
                mCurrentPageId=Self.Page.app_credit_card_intro_page;
                break;
            case 1:
                GoogleAnalyticsUtil.setScreenName(Google.LOANS_INRODUCE);
                mCurrentPageId=Self.Page.app_loan_intro_page;
                break;
            case 2:
                GoogleAnalyticsUtil.setScreenName(Google.SERVICE_AGREEMENT);
                mCurrentPageId=Self.Page.app_service_agreement_page;
                break;
            case 3:
                GoogleAnalyticsUtil.setScreenName(Google.SOCIAL_QUERY_AGREEMENT);
                mCurrentPageId=Self.Page.app_social_security_inquiry_agreement_page;
                break;
            case 4:
                GoogleAnalyticsUtil.setScreenName(Google.ACCUM_FUND_AGREEMENT);
                mCurrentPageId=Self.Page.app_provident_fund_inquiry_agreement_page;
                break;
            case 5:
                GoogleAnalyticsUtil.setScreenName(Google.LEARN_QUERY_AGREEMENT);
                mCurrentPageId=Self.Page.app_chsi_inquiry_agreement_page;
                break;
            case 6:
                GoogleAnalyticsUtil.setScreenName(Google.NEWS_DETAIL);
                mCurrentPageId=Self.Page.app_message_detail_page;
                break;
            case 7:
                GoogleAnalyticsUtil.setScreenName(Google.ABOUT_ME);
                mCurrentPageId= Self.Page.app_about_us_page;
                break;
            case 8:
                GoogleAnalyticsUtil.setScreenName(Google.CBB_AGREEMENT);
                mCurrentPageId=Self.Page.app_register_agreement_page;
                break;
            case 9:
                GoogleAnalyticsUtil.setScreenName(Google.CREDIT_PROGRESS_DETAIL);
                mCurrentPageId=Self.Page.app_card_progress_inquiry_page;
                break;
            case 10:
                GoogleAnalyticsUtil.setScreenName(Google.DEBIT_AGREEMENT);
                mCurrentPageId= Self.Page.app_payment_service_agreement;
                break;
            case 11:
                GoogleAnalyticsUtil.setScreenName(Google.COMMON_QUESTION);
                mCurrentPageId=Self.Page.app_common_question;
                break;
            case 12:
                mCurrentPageId=Self.Page.app_repayment_service_agreedment;
                break;
            case 13:
                mCurrentPageId=Self.Page.app_repayment_help;
                break;
            case 14:
                mCurrentPageId=Self.Page.app_chsi_auth_agreement_page;
                break;
            case 15:
                mCurrentPageId=Self.Page.app_provident_fund_auth_agreement_page;
                break;
            case 16:
                mCurrentPageId=Self.Page.app_social_security_auth_agreement_page;
                break;
            case 17:
                mCurrentPageId=Self.Page.app_operator_auth_agreement_page;
                break;
        }
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        ibtTitleLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Intent intent = getIntent();
        String title = intent.getStringExtra(WEB_TITLE_S);//标题
        mType = getIntent().getIntExtra(WEB_TYPE_I, -1);
        if (!TextUtils.isEmpty(title)) {
            tvTitle.setText(title);
        }
        String webUrl = intent.getStringExtra(WEB_URL_S);//网页地址
        if (!TextUtils.isEmpty(webUrl)) {
            showProgressDialog(false, "载入中...");
            setWebUrl(webUrl);
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_agreement_webview;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        llCommonContainer.removeView(mWebView);
        mWebView.removeAllViews();
        mWebView.destroy();
        mWebView = null;
    }

    /**
     * 设置网页
     */
    public void setWebUrl(String url) {
        WebSettings settings = mWebView.getSettings();
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setGeolocationEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setBlockNetworkImage(false);
        settings.setJavaScriptEnabled(true);
        settings.setTextSize(WebSettings.TextSize.NORMAL);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
            mWebView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        mWebView.requestFocus();
        mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        settings.setDefaultTextEncodingName("UTF-8");//避免中文乱码
        mWebView.loadUrl(url.startsWith("http") ? url : "http://" + url);
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                //加载完成
                dismissProgressDialog();
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
//                super.onReceivedSslError(view, handler, error);
                handler.proceed();
            }
        });
    }
}
